//
//  GoalSetupController.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/11.
//

import Foundation
import MYModule

class GoalSetupController: BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    override func makeUI() {
        super.makeUI()
        
        
        view.addSubview(self.scrollView)
        self.scrollView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }
        
        self.scrollView.addSubview(self.contentView)
        self.contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        self.contentView.addSubview(self.titleView)
        self.titleView.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
        }
        
        self.contentView.addSubview(self.normailGoalView)
        self.normailGoalView.snp.makeConstraints { make in
            make.top.equalTo(self.titleView.snp.bottom)
            make.centerX.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        self.contentView.addSubview(self.weeksGoalView)
        self.weeksGoalView.snp.makeConstraints { make in
            make.top.equalTo(self.normailGoalView.snp.bottom).offset(20)
            make.leading.trailing.bottom.equalToSuperview()
        }
        
    }

    override func bindViewModel() {
        super.bindViewModel()
        self.titleView.setupTitle(title: "user_goal_setting_title".language)
        
        self.viewModel.goalSetup.subscribe(onNext: { [ weak self] model in
            guard let self = self else { return }
            self.normailGoalView.bindModel(model: model.normalGoal)
            self.weeksGoalView.bindModel(model: model.weeksGoal)
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
    }
    
    override func backAction() {
        super.backAction()
        self.viewModel.setGoals()
    }
    
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        self.viewModel.setGoals()
        return true
    }
    
    //MARK: - lazy load -
    
    private lazy var viewModel = GoalSetupViewModel()
    
    private lazy var scrollView: ScrollView = {
        let view = ScrollView()
        view.showsVerticalScrollIndicator = false
        view.backgroundColor = UIColor.background
        view.bounces = false
        return view
    }()

    private lazy var contentView: View = {
        let view = View()
        view.backgroundColor = UIColor.background
        return view
    }()
    
    private lazy var titleView: GoalSetupTitleView = {
        let view = GoalSetupTitleView()
        view.backgroundColor = .assist_FFFFFF
        return view
    }()
    
    private lazy var normailGoalView: GoalSetupNormalView = {
        let view = GoalSetupNormalView()
        view.backgroundColor = .assist_FFFFFF
        view.cornerRadius = 18
        return view
    }()
    
    private lazy var weeksGoalView: GoalSetupWeeksView = {
        let view = GoalSetupWeeksView()
        view.backgroundColor = .assist_FFFFFF
        view.cornerRadius = 18
        view.weeksGoalSwitchView.switchView.delegate = self
        return view
    }()
}

extension GoalSetupController: CustomSwitchViewDelegate {
    func switchAction(isOn: Bool) {
        self.viewModel.model.weeksGoal.isOpen = isOn
        self.weeksGoalView.weekView.isHidden = !isOn
        self.weeksGoalView.weekGoalView.isHidden = !isOn
    }
}
